package sdu.attendancechecker.servlet.teacher;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import sdu.attendancechecker.dto.Question;
import sdu.attendancechecker.dto.Teacher;
import sdu.attendancechecker.models.TeacherModel;
import sdu.attendancechecker.services.AdminService;

public class HelpServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		Teacher t = (Teacher) session.getAttribute("teacher");
		if (request.getParameter("qid") != null) {
			int question_id = Integer.parseInt(request.getParameter("qid"));
			Question question = TeacherModel.getQuestionById(question_id);
			request.setAttribute("question", question);
			RequestDispatcher disp = request
					.getRequestDispatcher("one_question.jsp");
			disp.forward(request, response);
			return;
		}
		// response.sendRedirect("help.jsp");
		int teacher_id = t.getId();
		ArrayList<Question> questions = TeacherModel
				.getQuestionsByTeacherId(teacher_id);
		request.setAttribute("questions", questions);
		RequestDispatcher disp = request.getRequestDispatcher("help.jsp");
		disp.forward(request, response);

	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
			
			HttpSession session = request.getSession();
			Teacher t = (Teacher) session.getAttribute("teacher");
		
			System.out.println("post called");
			response.setContentType("text/html;charset=UTF-8");
			if(request.getParameter("command").equals("question")){
				if(request.getParameter("text")!=null){
					//String title="no title";
					String text=request.getParameter("text");
					//if(request.getParameter("title")!=null)
						//	title=request.getParameter("title");
					TeacherModel.addQuestionById(t.getId(),text);
					this.doGet(request, response);
				} else {
					response.getWriter().println(1);
					return;
				}
			}
	}

}
